home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / info-service / wais / ir-book-sources / mphf / regendrv.c < prev    next >
C/C++ Source or Header  |  1993-04-08  |  3KB  |  107 lines

  1. /*******************************  regendrv.c  ********************************
  2.  
  3.   Purpose:    A program to test regenerating and using a precomputed hashing
  4.           function.
  5.  
  6.   Provenance:    Written and tested by Q. Chen and E. Fox, April 1991.
  7.           Edited and tested by S. Wartik, April 1991.
  8.  
  9.   Notes:    The program is used as follows:
  10.  
  11.               regen_driver mphf-file keyword-file
  12.  
  13.         The result is a set of lines, written to stdout, indicating
  14.         the bucket of each keyword in the keyword file.
  15. **/
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <math.h>
  20.  
  21. #include "types.h"
  22. #include "rantab.h"
  23. #include "regenphf.h"    
  24.  
  25. #ifdef __STDC__
  26.  
  27. extern void    retrieveAll ( mphfType *mphf, char *key_file );
  28.  
  29. extern void    exit( int status );
  30.  
  31. #else
  32.  
  33. extern void    retrieveAll ();
  34.  
  35. extern void    exit();
  36.  
  37. #endif
  38.  
  39. /**************************************************************************
  40.  
  41.        main( int, char** )
  42.  
  43.    Return:    Nothing.
  44.  
  45.    Purpose:    See the header for this file.
  46. **/
  47.  
  48. main( argc, argv )
  49.    int argc;
  50.    char *argv[];            /* arg1: mphf file;  arg2: key file */ 
  51. {
  52.     mphfType mphf;
  53.  
  54.     if ( argc != 3 ) {
  55.     fprintf(stderr, "Usage: %s mphf-file key-file\n", argv[0]);
  56.     exit(1);
  57.     }
  58.  
  59.     if ( regen_mphf ( &mphf, argv[1] ) == NORM ) 
  60.     retrieveAll ( &mphf, argv[2] );
  61.     else {
  62.     fprintf(stderr, "Can't regenerate hashing function from \"%s\".\n",
  63.         argv[1]);
  64.     exit(1);
  65.     }
  66.  
  67.     release_mphf ( &mphf );
  68.     exit(0);
  69. }
  70.  
  71. /**************************************************************************
  72.  
  73.        retrieveAll( mphfType*, char* )
  74.  
  75.    Return:    void
  76.  
  77.    Purpose:    Given a file of keys and a structure describing a
  78.            MPHF previously computed for those keys, print
  79.         each key's location on the standard output stream.
  80. **/
  81.  
  82. void retrieveAll( mphf, key_file )
  83.     mphfType    *mphf;            /* in: mphf specification.      */
  84.     char    *key_file;        /* in: the key file.        */
  85. {
  86.     FILE    *fp;                      /* Handle for specification file. */
  87.     char    string[MAX_KEY_LENG];     /* Key string.                  */
  88.     int        hash;                      /* Computed hash value.         */
  89.     int        max_bucket_length;    /* The maximum number of chars  */
  90.                         /* needed to represent a bucket */
  91.                         /* index as a string.        */
  92.  
  93.     if ( (fp = fopen(key_file, "r")) == 0 ) {
  94.     fprintf(stderr, "Can't read file \"%s\".\n", key_file);
  95.     exit(1);
  96.     }
  97.  
  98.     max_bucket_length = (int)log10((double)mphf->no_arcs) + 1;
  99.     while ( fgets( string, MAX_KEY_LENG, fp ) != 0 ) {
  100.     string[strlen(string)-1] = '\0';
  101.     hash = retrieve( mphf, string );
  102.     printf("Bucket %*d: %s\n", max_bucket_length, hash, string);
  103.     }
  104.  
  105.     fclose(fp);
  106. }
  107.